package com.leps.nq.bu.controller;

import cn.hutool.core.util.ObjectUtil;
import com.leps.nq.bu.entity.BuPlc;
import com.leps.nq.bu.entity.BuWell;
import com.leps.nq.bu.service.IBuPlcService;
import com.leps.nq.bu.service.IBuWellService;
import com.leps.nq.bu.vo.WellQueryModel;
import com.leps.nq.bu.vo.WellVo;
import com.leps.nq.common.aspect.Log;
import com.leps.nq.common.enums.BusinessType;
import com.leps.nq.common.vo.PageVo;
import com.leps.nq.common.vo.R;
import com.leps.nq.common.vo.RBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 井表 前端控制器
 * </p>
 *
 * @author hex
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/bu/bu-well")
@Api(tags = "井管理")
public class BuWellController {
    @Autowired
    private IBuWellService wellService;
    @Autowired
    private IBuPlcService plcService;

    @GetMapping("/forSelect")
    @ApiOperation("井下拉列表")
    public R<List<BuWell>> listForSelect(Integer pumpId) {
        List<BuWell> list = this.wellService.lambdaQuery().eq(pumpId != null, BuWell::getPumpId, pumpId).list();
        return RBuilder.ok(list);
    }

    @GetMapping
    @ApiOperation("井列表")
    public R<PageVo<WellVo>> list(WellQueryModel queryModel) {
        PageVo<WellVo> pageVo = this.wellService.pageByQueryModel(queryModel);
        return RBuilder.ok(pageVo);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询井信息")
    public R<BuWell> getById(@PathVariable Integer id) {
        return RBuilder.ok(this.wellService.getById(id));
    }

    @PostMapping
    @ApiOperation("添加井")
    @Transactional
    @Log(title = "井", businessType = BusinessType.INSERT)
    public R<?> save(@RequestBody BuWell well) {
        this.wellService.save(well);
        final int pumpId = well.getPumpId();
        final int plcId = well.getPlcId();
        this.updatePlcType2(pumpId, plcId);
        return RBuilder.ok();
    }

    @ApiOperation("批量添加井")
    @PostMapping("/batch")
    @Transactional
    @Log(title = "井", businessType = BusinessType.INSERT)
    public R saveBatch(@RequestBody List<BuWell> wells) {
        if (ObjectUtil.isEmpty(wells)) return RBuilder.ok();
        for (BuWell well : wells) {
            this.wellService.save(well);
        }
        final int pumpId = wells.get(0).getPumpId();
        final int plcId = wells.get(0).getPlcId();
        this.updatePlcType2(pumpId, plcId);
        return RBuilder.ok();
    }

    private void updatePlcType2(int pumpId, int plcId) {
        Long wellNumberOfPump = this.wellService.lambdaQuery().eq(BuWell::getPumpId, pumpId).count();
        if (wellNumberOfPump > 1) {
            BuPlc plc = this.plcService.getById(plcId);
            String plcType2 = plc.getPlcType2();
            if ("撬装".equals(plcType2)) {
                plcType2 = "撬装一泵多注";
            } else if ("单泵".equals(plcType2)) {
                plcType2 = "一泵多注";
            }
            this.plcService.lambdaUpdate().set(BuPlc::getPlcType2, plcType2).eq(BuPlc::getPlcId, plcId).update();
        }
    }

    @PutMapping
    @ApiOperation("更新井")
    @Log(title = "井", businessType = BusinessType.UPDATE)
    public R<?> update(@RequestBody BuWell well) {
        this.wellService.updateById(well);
        return RBuilder.ok();
    }

    @DeleteMapping
    @ApiOperation("删除井")
    @Log(title = "井", businessType = BusinessType.DELETE)
    public R<?> remove(@RequestBody List<Integer> wellIds) {
        this.wellService.removeBatchByIds(wellIds);
        return RBuilder.ok();
    }
}
